package com.bank.service;

import com.bank.dao.AdminDAO;
import com.bank.dao.CustomerDAO;
import com.bank.dao.DBUtil;
import com.bank.entity.Customer;

import java.sql.*;

public class AuthService {

    /**
     * 管理员登录验证
     */
    public static boolean adminLogin(String adminID, String password) {
        try {
            return AdminDAO.login(adminID, password);
        } catch (SQLException e) {
            System.err.println("管理员登录失败: " + e.getMessage());
            return false;
        }
    }

    /**
     * 测试数据库连接状态
     */
    public static void testDBConnection() {
        System.out.println("开始数据库连接测试...");

        try (Connection conn = DBUtil.getConnection()) {
            if (conn != null && !conn.isClosed()) {
                System.out.println("✅ 数据库连接成功");
                System.out.println("数据库产品: " + conn.getMetaData().getDatabaseProductName());
                System.out.println("数据库版本: " + conn.getMetaData().getDatabaseProductVersion());

                // 修改为查询 admin 表
                try (Statement stmt = conn.createStatement();
                     ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM admin")) {
                    if (rs.next()) {
                        System.out.println("admin 表记录数: " + rs.getInt(1));
                    }
                } catch (SQLException e) {
                    System.err.println("查询 admin 表失败: " + e.getMessage());
                }
            } else {
                System.out.println("❌ 数据库连接失败");
            }
        } catch (SQLException e) {
            System.err.println("数据库连接异常: " + e.getMessage());
            System.err.println("SQL状态: " + e.getSQLState());
            System.err.println("错误代码: " + e.getErrorCode());
        }
    }

    /**
     * 客户登录验证
     */
    // AuthService.java (不依赖getCustomerByID的简化版)
    public static int customerLogin(String name, String pid) {
        try {
            // 假设getCustomerId方法返回-1表示未找到用户
            int customerId = CustomerDAO.getCustomerId(name, pid);
            return customerId; // 直接返回customerId，不再验证PID
        } catch (SQLException e) {
            return -1;
        }
    }
    }
